package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbContentCategory;
import com.baike.service.ITbContentCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 内容分类 前端控制器
 * </p>
 *
 * @author x_FG
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/tbContentCategory")
@Api(value = "广告分类查询接口")
@RefreshScope
public class TbContentCategoryController {

    @Autowired
    private ITbContentCategoryService contentCategoryService;

    @ApiOperation("广告分类查询方法")
    @GetMapping
    public Result getCategoryInfo(){
        List<TbContentCategory> list = contentCategoryService.list();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation("根据Id查询分类表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "TbContentCategory实体类字段id",required = true,paramType = "path")
    })
    @GetMapping("ID/{id}")
    public Result getCategoryById(@PathVariable("id")Integer id){
        TbContentCategory contentCategory = contentCategoryService.getCategoryById(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(contentCategory);
    }

    @ApiOperation("广告分类增加方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "分类名称",required = true,paramType = "path"),
            @ApiImplicitParam(name = "price",value = "价格",required = true,paramType = "path")
    })
    @PostMapping("/{name}/{price}")
    public Result insertCategoryInfo(@PathVariable("name")String name,@PathVariable("price")Double price){
        TbContentCategory contentCategory = new TbContentCategory();
        contentCategory.setName(name).setPrice(price);
        boolean b = contentCategoryService.insertCategoryInfo(contentCategory);
        if (b){

            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("广告分类修改方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "广告分类id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "name",value = "分类名称",required = true,paramType = "path"),
            @ApiImplicitParam(name = "price",value = "价格",required = true,paramType = "path")
    })
    @PutMapping("/{id}/{name}/{price}")
    public Result updateCategoryInfo(@PathVariable("name")String name,@PathVariable("price")Double price,
                                     @PathVariable("id")Long id){
        TbContentCategory contentCategory = new TbContentCategory();
        contentCategory.setName(name).setPrice(price).setId(id);
        boolean b = contentCategoryService.updateCategoryInfo(contentCategory);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("广告分类表根据Id 删除方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "TbContentCategory实体类字段id",required = true,paramType = "path")
    })
    @DeleteMapping("ID/{id}")
    public Result deleteCategoryById(@PathVariable("id")Integer[] id){
        boolean b = contentCategoryService.removeByIds(Arrays.asList(id));
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

}
